package com.iuie.ns.frame.web.response.vo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.shiro.authc.UsernamePasswordToken;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iuie.ns.frame.web.dataset.vo.Page;

/**
 * 响应信息
 * @author lj
 * @createTime 2017年1月1日 下午12:48:49
 */
public class ResponseInfo {
	
	public static final String CODE_SUCCESS = "0000";

	private boolean success = true; // 默认为true
	
	private String message;
	
	private String exceptionStack;
	
	private String code = CODE_SUCCESS;
	
	private Object info;
	
	public ResponseInfo(){
		
	}
	
	public ResponseInfo(Object info) {
	    this.info = info;
	}
	
	public ResponseInfo(Object info, String message) {
	    this(info);
	    this.message = message;
	}
	
	public ResponseInfo(boolean success,String message){
		this.message = message;
		this.success = success;
	}
	
	public static void main(String[] args) {
		Page page = new Page();
		page.setCount(150);
		page.setRows(10);
		page.setPage(1);
		List<UsernamePasswordToken> list = new ArrayList<UsernamePasswordToken>();
		for (int i = 0; i < 10; i++) {
			UsernamePasswordToken token = new UsernamePasswordToken();
			token.setHost("127.0.0.1");
			token.setPassword(("sdfjisj"+i*i+"fjskdj"+Math.PI*i).toCharArray());
			token.setUsername(String.valueOf(Math.PI*i*i));
			token.setRememberMe(i%2==1);
			list.add(token);
		}
		ResponseInfo ri = new ResponseInfo(page, list);
		String json = ri.toJson();
		System.out.println(json);
		
	}
	
	public ResponseInfo(Page page,List<?> list){
		Map<String, Object> infoMap = new HashMap<String, Object>();
		infoMap.put("count", page.getCount());
		infoMap.put("data", list);
		this.info = infoMap;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public ResponseInfo setMessage(String message) {
		this.message = message;
		return this;
	}

	public Object getInfo() {
		return info;
	}

	public void setInfo(Object info) {
		this.info = info;
	}
	
	public String getCode() {
        return code;
    }
	
	public void setCode(String code) {
        this.code = code;
    }
	
	public void setExceptionStack(String exceptionStack) {
        this.exceptionStack = exceptionStack;
    }

	public String toJson(){
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
			String json = mapper.writeValueAsString(this);
			return json;
		} catch (JsonProcessingException e) {
			this.message = "ResponseInfo 转化 JSON 失败，失败原因： " + e.getMessage();
			e.printStackTrace();
		}
		return errorJson(this.message);
	}
	
	/**
	 * 当转化json失败时，直接返回一个错误信息
	 * @createTime 2017年1月2日 下午8:14:42
	 * @param message
	 * @return
	 */
	public static String errorJson(String message) {
		ResponseInfo info = new ResponseInfo();
		info.message=message;
		info.success=false;
		info.info=null;
		return info.toJson();
	}
	
	public static ResponseInfo errorInfo(String message) {
		ResponseInfo info = new ResponseInfo();
		info.message=message;
		info.success=false;
		info.info=null;
		return info;
	}
	
	public static String errorJson(String message, String code) {
        ResponseInfo info = new ResponseInfo();
        info.message=message;
        info.success=false;
        info.info=null;
        info.code = code;
        return info.toJson();
    }
	
	public static ResponseInfo errorInfo(String message, String code) {
		ResponseInfo info = new ResponseInfo();
		info.message=message;
		info.success=false;
		info.info=null;
		info.code = code;
		return info;
	}
	
    public static String errorJson(String message, String code, Object info) {
        ResponseInfo ri = new ResponseInfo();
        ri.message = message;
        ri.success = false;
        ri.info = info;
        ri.code = code;
        return ri.toJson();
    }
    
    public static ResponseInfo errorInfo(String message, String exceptionStack, String code, Object info) {
		ResponseInfo ri = new ResponseInfo();
		ri.message = message;
		ri.success = false;
		ri.info = info;
		ri.code = code;
		return ri;
	}
    
    public static ResponseInfo successInfo(String message) {
    	ResponseInfo ri = new ResponseInfo();
    	ri.message = message;
    	return ri;
    }
    
    public static ResponseInfo successInfo(Object info) {
		ResponseInfo ri = new ResponseInfo();
		ri.success = true;
		ri.info = info;
		return ri;
	}
    
    public static ResponseInfo successInfo(String message, Object info)  {
        ResponseInfo ri = new ResponseInfo(message);
        ri.info = info;
        return ri;
    }
    
    public static ResponseInfo successInfo(List<?> list, Page page) {
		ResponseInfo ri = new ResponseInfo();
		ri.success = true;
		Map<String, Object> infoMap = new HashMap<String, Object>();
		infoMap.put("count", page.getCount());
		infoMap.put("data", list);
		ri.info = infoMap;
		return ri;
	}
    
    public Map<String, Object> toMap(boolean hasExceptionStack) {
		Map<String, Object> map = new HashMap<>();
		map.put("code", this.code);
		map.put("message", this.message);
		map.put("info", this.info);
		map.put("success", this.success);
		if (hasExceptionStack){
			map.put("exceptionStack", this.exceptionStack);
		}
		return map;
	}
}
